package com.csdn.eval.spring.mqtt;

import com.csdn.eval.spring.mqtt.annotation.MqttEventListenerAnnotationBeanPostProcessor;
import com.csdn.eval.spring.mqtt.config.EvalSpringMqttProperties;
import com.csdn.eval.spring.mqtt.core.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.config.EnableIntegration;

@Configuration
@ConditionalOnProperty(name = "eval.spring.mqtt.enabled", havingValue = "true")
@EnableIntegration
@IntegrationComponentScan("com.csdn.eval.spring.mqtt")
@EnableConfigurationProperties(EvalSpringMqttProperties.class)
@ComponentScan(basePackages = "com.csdn.eval.spring.mqtt")
public class EvalSpringMqttAutoConfiguration {

    @Bean("AnnotationDispatcherMessageHandler")
    @ConditionalOnMissingBean(name = "AnnotationDispatcherMessageHandler")
    public MessageHandlerService messageHandlerService() {
        return new AnnotationDispatcherMessageHandler();
    }

    @Bean("DefaultDispatcherMessageHandler")
    @ConditionalOnMissingBean(name = "DefaultDispatcherMessageHandler")
    public MessageHandlerService defaultDispatcherMessageHandler() {
        return new DefaultDispatcherMessageHandler();
    }

    @Bean
    @ConditionalOnMissingBean
    public TopicSubscribeService topicSubscribeService() {
        return new DefaultTopicSubscribe();
    }

    @Bean
    public MqttEventFactory mqttEventFactory() {
        return new MqttEventFactory();
    }

    @Bean
    public MqttEventListenerAnnotationBeanPostProcessor beanPostProcessor(MqttEventFactory mqttEventFactory) {
        return new MqttEventListenerAnnotationBeanPostProcessor(mqttEventFactory);
    }

    @Bean
    public MqttSubscriber mqttSubscriber() {
        return new MqttSubscriber();
    }

    @Bean
    public MqttGatewayPublish mqttGatewayPublish() {
        return new MqttGatewayPublish();
    }
}